const BillDao = require("../../dao/bill/bill_dao.js");

const BillService = {
    add(req, res, next) {
        const {
            proname,
            prounit,
            supplier,
            number,
            money,
            // supplier,
            flag,
            datatime
        } = req.body;

        BillDao.save({
                proname,
                prounit,
                supplier,
                number,
                money,
                // supplier,
                flag,
                datatime
            })
            .then((data) => {
                res.json({
                    res_code: 1,
                    res_error: "",
                    res_body: {
                        status: 1,
                        data: data
                    }
                })
            })
            .catch((err) => {
                res.json({
                    res_code: 0,
                res_error: err,
                res_body: {}
            })
            })
    },

    findByPage(req, res, next) {
        const {
            page
        } = req.query;

        BillDao.findbyPage(page)
            .then((data) => {
                res.json({
                    res_code: 1,
                    res_error: "",
                    res_body: {
                        status: 1,
                        list: data
                    }
                })
            })
            .catch((err) => {
                res.json({
                    res_code: 0,
                    res_error: err,
                    res_body: {}
                })
            })
    },

    delete(req, res, next) {
        const {
            _id
        } = req.body;

        BillDao.delete({
                _id
            })
            .then(() => {
                res.json({
                    res_code: 1,
                    res_error: "",
                    res_body: {
                        status: 1,
                    }
                })
            })
    },

    update(req,res,next){
        const {_id,proname,prounit,supplier, number,money,flag,datatime} = req.body;
        
        BillDao.update(_id,{proname,prounit,supplier, number,money,flag,datatime})
            .then((data)=>{
                res.json({
                    res_code:1,
                    res_error:"",
                    res_body:{
                        status:1,
                        data:data
                    }
                })
            })
    },
    findbyname(req,res,next){
        const {proname} = req.query;
        
        BillDao.find({proname})
            .then((data)=>{
                if(data.length >= 1){
                    // console.log(data);
                    res.json({
                        res_code: 1,
                        res_error: "",
                        res_body: {
                            status: 1,
                            message: "success",
                            data: data
                        }
                    })
                }
            })
    }
}
module.exports = BillService;